/*
 * SAMainContainer.cpp
 *
 *  Created on: 2010-06-04
 *      Author: NONO
 */

// System includes

#include <StringLoader.h>       // StringLoader
#include <SMSAssist_0xEB692DC8.rsg>
#include <BARSREAD.h>
#include <aknutils.h>

// User includes
#include "SA.hrh"
#include "SAAppUi.h"

// CLASS INCLUDE
#include "SAMainContainer.h"

// CONSTANTS
#define KLTPoint    TPoint(0,0)


// ================= MEMBER FUNCTIONS =======================

// ---------------------------------------------------------
// Constructor, destructor
// ---------------------------------------------------------
void CSAMainContainer::ConstructL(const TRect& aRect)
{
    CreateWindowL();
    CreateMsgListL();
    // Call parent class's ConstructL
    CSABaseContainer::ConstructL(aRect);
}

CSAMainContainer::CSAMainContainer(CSAAppUi& aAppUi):CSABaseContainer(aAppUi)
{
}


// Destructor
CSAMainContainer::~CSAMainContainer()
{
    // Delete UI components
    delete iMsgboxListBox;
}

void CSAMainContainer::ChangeViewMenuL(TInt aResourceId)
{
    CSABaseContainer::ChangeViewMenuL(TUid::Uid(ESAMainViewId), aResourceId);
}

void CSAMainContainer::SizeChanged()
{
    #if defined(__SERIES60_28__) || defined(__SERIES60_3X__)
        // Scalable UI support
        TRect rect;
        AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
        iMsgboxListBox->SetExtent(KLTPoint, rect.Size());
    #else
        iMsgboxListBox->SetExtent(KLTPoint, iProtectionListBox->MinimumSize());
    #endif
}


// ---------------------------------------------------------
// CSAMainContainer::CountComponentControls() const
// ---------------------------------------------------------
TInt CSAMainContainer::CountComponentControls() const
{
    return 1;
}

// ---------------------------------------------------------
// CSAMainContainer::ComponentControl(TInt aIndex) const
// ---------------------------------------------------------
CCoeControl* CSAMainContainer::ComponentControl(TInt aIndex) const
{
    switch ( aIndex )
    {
        case 0:
            return iMsgboxListBox;
        default:
            return NULL;
    }
}

// ---------------------------------------------------------
// CSAMainContainer::HandleResourceChange()
// Called by framework when layout is changed.
// ---------------------------------------------------------
//
void CSAMainContainer::HandleResourceChange(TInt aType)
{
    CCoeControl::HandleResourceChange(aType);

    #if defined(__SERIES60_28__) || defined(__SERIES60_3X__)
        // ADDED FOR SCALABLE UI SUPPORT
        // *****************************
        if ( aType==KEikDynamicLayoutVariantSwitch )
            {
            TRect rect;
            AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
            SetRect(rect);
            }
            
        if(iMsgboxListBox)
            iMsgboxListBox->HandleResourceChange(aType);
    #endif
}


// ---------------------------------------------------------
// CSAMainContainer::Draw(const TRect& aRect) const
// ---------------------------------------------------------
//
void CSAMainContainer::Draw(const TRect& aRect) const
{
    CWindowGc& gc = SystemGc();
    // TODO: Add your drawing code here
//    gc.SetPenStyle( CGraphicsContext::ENullPen );
//    gc.SetBrushColor( KRgbGray );
//    gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
//    gc.DrawRect( aRect );
}

TKeyResponse CSAMainContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
    TKeyResponse response = iSAAppUi.HandleKeyEventL(aKeyEvent, aType);
//    if (response == EKeyWasConsumed)
//        return response;
    if (response == EKeyWasNotConsumed)
        response = iMsgboxListBox->OfferKeyEventL(aKeyEvent, aType);

    return response;
}

void CSAMainContainer::HandleControlEventL(CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
{
}

void CSAMainContainer::CreateMsgListL()
{
    // First phase construction
    iMsgboxListBox = new (ELeave) CAknDoubleLargeStyleListBox;
    iMsgboxListBox->SetContainerWindowL(*this);
    
    // Second Phase Construction
    TResourceReader reader;
    iEikonEnv->CreateResourceReaderLC(reader, R_SA_MAINVIEW_LISTBOX);
    iMsgboxListBox->ConstructFromResourceL(reader);
    CleanupStack::PopAndDestroy(); // reader

    // Setup icons and scrollbar
    
    SetupListIconsL(*iMsgboxListBox, 1);    // TBM!
    
    iMsgboxListBox->CreateScrollBarFrameL(ETrue);
    iMsgboxListBox->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);

    SetupEmptyListPromptL(*iMsgboxListBox, R_SA_EMPTY_LISTBOX);

    // Setup list items 
    SetupMsgboxListItemsL();
}

void CSAMainContainer::SetupMsgboxListItemsL()
{
    CTextListBoxModel* model = iMsgboxListBox->Model();  // not taking ownership
    model->SetOwnershipType(ELbmOwnsItemArray);
    CDesCArray* savedListsArray = STATIC_CAST(CDesCArray*, model->ItemTextArray());
//    LoadSavedListsL(*savedListsArray);
}

void CSAMainContainer::SetupListIconsL(CEikFormattedCellListBox& aListBox, TInt aIndex)
{
    // TODO
}

void CSAMainContainer::SetupEmptyListPromptL(CEikListBox& aListBox, const TInt aResourceId)
{
    if (aResourceId)
    {
        CListBoxView* view = aListBox.View();
        if (view)
        {
            HBufC* emptyList = StringLoader::LoadLC(aResourceId);
            aListBox.View()->SetListEmptyTextL(*emptyList);
            CleanupStack::PopAndDestroy(emptyList);
        }
    }
}
// End of file
